package eduAndExpRequire;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class EduAndExpMapper extends Mapper<Object, Text, Text, IntWritable> {

    private final static Text keyOut = new Text();
    private final static IntWritable valueOut = new IntWritable();

    @Override
    protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

        String[] fields = value.toString().split(",");

        String eduBg = fields[5];
        String expRQ = fields[8];

        int expInt = getExperienceInYears(expRQ);

        if (eduBg.equals("edu")) {
            return;
        }

        keyOut.set(eduBg);
        valueOut.set(expInt);

        context.write(keyOut, valueOut);
    }

    // 将经验要求转成最大的年数
    public static int getExperienceInYears(String experienceReq) {
        String extraS = "年";
        String conectS = "-";

        if (experienceReq.contains(extraS)) {
            // 区间，如 "1-2年"
            if (experienceReq.contains(conectS)) {
                String[] parts = experienceReq.split(conectS);
                return Integer.parseInt(parts[1].replace(extraS, ""));
            } else {
                // 单一年份，如 "x年"
                return Integer.parseInt(experienceReq.replace(extraS, ""));
            }
        }
        return 0;
    }
}
